package mspbots.timedoctor.client.core;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import mspbots.timedoctor.client.core.api.SyncDataLog;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;

import java.time.LocalDateTime;

/**
 * com.mspbots.web.utils.HttpRequestParamsUtils
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/8/26
 */
@Log4j2
public abstract class BaseGenericService {

    protected final String REDIS_KEY_PREFIX = "data-connector:time-doctor:";
    protected final String USERS_ALL = "v2:usersall:";
    protected final String COMPANIES = "v2:companies:";
    protected final String FOLDERS = "v2:folders:";
    protected final String PROJECTS = "v2:projects:";
    protected final String TASKS = "v2:tasks:";
    protected final String WORKLOG = "v2:worklog:";
    protected final String ONOFF = "v2:onoff:";

    protected final int SUCCESS_CODE = 200;
    protected final int FAIL_CODE = 500;


    protected RabbitMessagingTemplate messagingTemplate;
    protected ReactiveStringRedisTemplate stringRedisTemplate;
    protected ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;
    protected ObjectMapper objectMapper;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setMessagingTemplate(RabbitMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    @Autowired
    public void setStringRedisTemplate(ReactiveStringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Autowired
    public void setReactiveRedisTemplate(ReactiveRedisTemplate<String, Object> redisTemplate) {
        this.reactiveRedisTemplate = redisTemplate;
    }

    protected void sendLog(String sync_id, Long tenantId, long milliseconds, String interfaceName, int status, String msg, JsonNode extend) {
        this.messagingTemplate.convertAndSend("data.exception.log",
                SyncDataLog.builder()
                        .syncId(sync_id)
                        .tenantId(tenantId)
                        .milliseconds(milliseconds)
                        .serviceName("time-doctor")
                        .interfaceName(interfaceName)
                        .status(status)
                        .msg(msg)
                        .logTime(LocalDateTime.now())
                        .extend(extend)
                        .build());
    }
}
